package net.ib.mtalk.ui.buddy.group;

import java.util.HashMap;
import java.util.Map;

import net.ib.mtalk.R;
import net.ib.mtalk.database.DatabaseContract.GroupTable;
import net.ib.mtalk.database.DatabaseContract.ParticipantsTable;
import net.ib.mtalk.database.helper.GroupDatabaseHelper;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;

public class EditGroupCursorAdapter extends CursorAdapter{
	
	private int mLayout;
	private LayoutInflater mInflater;
	private OnClickListener mListener;
	
	public EditGroupCursorAdapter(Context context, Cursor cursor, int flags, OnClickListener listener){
		super(context, cursor, flags);
		
		mLayout = R.layout.item_add_group;
		this.mListener = listener;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		mInflater = LayoutInflater.from(context);
		View v = mInflater.inflate(mLayout, parent, false);
		
		ViewHolder holder = new ViewHolder();
		holder.editText = (EditText) v.findViewById(R.id.iag_edit);
		holder.button = (Button) v.findViewById(R.id.iag_delete_btn);
		
		v.setTag(holder);
		return v;
	}
	
	private static class ViewHolder {
		EditText editText;
		Button button;
	}

	@Override
	public void bindView(View view, final Context context, Cursor cursor) {
		ViewHolder holder = (ViewHolder)view.getTag();
		
		final int groupId = cursor.getInt(cursor.getColumnIndexOrThrow(GroupTable._ID));
		
		holder.editText.setText(cursor.getString(cursor.getColumnIndexOrThrow(GroupTable.KEY_GROUP_NAME)));
		holder.editText.setTag(groupId);
		holder.editText.setOnClickListener(mListener);
		holder.button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				removeGroup(context, v, groupId);
			}
		});
	}
	private void removeGroup(Context context, View v, final int id) {
		String selection = ParticipantsTable.KEY_PARTICIPANTS_GROUP + "=?";
		String[] selectionArgs = {String.valueOf(id)};
		Cursor cursor = context.getContentResolver().query(ParticipantsTable.buildBuddyGroupUri(), null, selection, selectionArgs, null);
		
		if(cursor.getCount() > 0 ){
			AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
			builder.setMessage(R.string.group_delete_confirm1);
			builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					GroupDatabaseHelper.deleteGroup(mContext, id);
				}
			});
			builder.setNegativeButton(R.string.cancel, null);
			builder.show();
		} else {
			AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
			builder.setMessage(R.string.group_delete_confirm2);
			builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					GroupDatabaseHelper.deleteGroup(mContext, id);
				}
			});
			builder.setNegativeButton(R.string.cancel, null);
			builder.show();
		}
	}
}
